<template>
  <div class="race">
    <div class="header">
      <table>
        <tr>
          <td>
            <span>服务器：</span>
            <Server ref="ServerChild" :showServer="showServer" />
          </td>
          <td>
            <span>开始时间：</span>
            <el-date-picker
              v-model="first_time"
              type="datetime"
              value-format="timestamp"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </td>
          <td>
            <span>结束时间：</span>
            <el-date-picker
              v-model="last_time"
              type="datetime"
              value-format="timestamp"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </td>
          <td>
            <el-button type="primary" @click="search()">查询</el-button>
          </td>
        </tr>
      </table>
    </div>
    <div class="content">
      <el-main v-loading="loading">
        <el-table
          border
          :fit="true"
          :header-cell-style="{
            color: '#333',
            fontFamily: 'MicrosoftYaHeiUI',
            fontWeight: 500,
            fontSize: '16px',
          }"
          :data="tableData.slice((currentPage - 1) * pageSize,currentPage * pageSize)"
        >
          <el-table-column
            prop="server_id"
            sortable
            label="服务器"
          ></el-table-column>
          <el-table-column
            prop="race_draw_times"
            sortable
            label="种族抽次数"
          ></el-table-column>
          <el-table-column
            prop="water_frequency"
            sortable
            label="水次数"
          ></el-table-column>
          <el-table-column
            prop="water_population"
            sortable
            label="水人数"
          ></el-table-column>
          <el-table-column
            prop="number_of_fires"
            sortable
            label="火次数"
          ></el-table-column>
          <el-table-column
            prop="number_of_fire_victims"
            sortable
            label="火人数"
          ></el-table-column>
          <el-table-column
            prop="wind_frequency"
            sortable
            label="风次数"
          ></el-table-column>
          <el-table-column
            prop="wind_number"
            sortable
            label="风人数"
          ></el-table-column>
          <el-table-column
            prop="times"
            sortable
            label="地次数"
          ></el-table-column>
          <el-table-column
            prop="number_of_people"
            sortable
            label="地人数"
          ></el-table-column>
        </el-table>
        <pageNation
          :total="tableData.length"
          :page-size="pageSize"
          @pageChange="pageChange"
          :page_index="currentPage"
        />
      </el-main>
    </div>
  </div>
</template>
<script>
import Server from "../../components/server/Server.vue";
import pageNation from "../../components/pagenation/pageNation";
let map = new Map();
export default {
  name: "Race",
  components: { Server, pageNation },
  data() {
    return {
      showServer: false,
      tableData: [],
      first_time: "",
      last_time: "",
      pageSize: 10,
      currentPage: 1,
      loading: false,
    };
  },
  mounted() {
    this.first_time = window.localStorage.getItem("first_time");
    this.last_time = window.localStorage.getItem("last_time");
    //获取服务器名称
    this.$http.get("/api/gmtool/getServer").then((res) => {
      res.data.result.forEach((item) => {
        map.set(item.serverId, item.serverName);
      });
    });
  },
  methods: {
    //分页
    pageChange(item) {
      this.currentPage = item.page_index;
      this.pageSize = item.page_limit;
    },
    //查询
    search() {
      var last_time = this.last_time;
      var first_time = this.first_time;
      window.localStorage.setItem("last_time", last_time);
      window.localStorage.setItem("first_time", first_time);
      var serverIdArr=this.$refs.ServerChild.serverIdArr
      if (serverIdArr.length==0) {
        this.$message({
          type: "warning",
          message: "请选择需要查询的服务器名称",
        });
        return;
      }
      if (first_time / 1000 == "") {
        this.$message({ type: "warning", message: "请输入开始时间" });
        return;
      }
      if (last_time / 1000 == "") {
        this.$message({ type: "warning", message: "请输入结束时间" });
        return;
      }
      var arr = [];
      serverIdArr.forEach((itemserver) => {
        if (itemserver == "全选") {
          return;
        } else {
          this.loading = true;
          this.$http.post("/api/payment/race", {
              server_id: itemserver,
              last_time: last_time / 1000,
              first_time: first_time / 1000,
          }).then((res) => {
              if (res.data.code == 200) {
                res.data.result.forEach((item) => {
                  let obj = {
                    server_id: map.get(item.server_id),
                    race_draw_times: item.race_draw_times,
                    water_frequency: item.water_frequency,
                    water_population: item.water_population,
                    number_of_fires: item.number_of_fires,
                    number_of_fire_victims: item.number_of_fire_victims,
                    wind_frequency: item.wind_frequency,
                    wind_number: item.wind_number,
                    times: item.times,
                    number_of_people: item.number_of_people,
                  };
                  arr.push(obj);
                });
                this.tableData = arr;
                this.loading = false;
              } else {
                this.loading = false;
                return;
              }
            });
        }
      });
    }
  },
};
</script>
<style scoped>
.race {
  width: 100%;
  height: auto;
  background-color: #fff;
}
.race td {
  padding: 18px;
  font-size: 14px;
  display: flex;
  float: left;
}
</style>